package com.finance.cooperate.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.finance.cooperate.common.utils.DozerMapper;
import com.finance.cooperate.dao.entity.primary.Sms;
import com.finance.cooperate.dao.local.scope.CooperateRequestScope;
import com.finance.cooperate.dao.repository.primary.SmsRepository;
import com.finance.cooperate.data.dto.SmsDTO;
import com.finance.cooperate.data.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SmsServiceImpl
 * @Description TODO
 * @Author shen
 * @Date 2023/8/10 21:36
 * @Modify ...
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsRepository smsRepository;

    @Autowired
    private CooperateRequestScope requestScope;


    @Override
    public String getSmsList(String uid) {


        String packageName = requestScope.getPackageName();
        String deviceId = requestScope.getDeviceId();

        Long loanNum = requestScope.getLoanNum();

        // 跑特征取新数
        Date lastSubmitTime = requestScope.getLastSubmitTime();

        List<Sms> smsList = new ArrayList<>();

        if (loanNum > 1) {

            Date lastTime = smsRepository.getSmsLastTime(deviceId, packageName, lastSubmitTime);

            smsList = smsRepository.getSms3Month(deviceId, packageName, lastTime);

        } else {
            smsList = smsRepository.getFirstSms3Month(deviceId, packageName, lastSubmitTime);
        }


        List<SmsDTO> smsDTOS = DozerMapper.mapList(smsList, SmsDTO.class);

        return JSON.toJSONString(smsDTOS);
    }
}
